 package com.SparkCore.RDD.Operator.Transform


import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import java.text.SimpleDateFormat



 /**
 *从服务器日志数据 apache.log 中获取每个时间段访问量。
  */
object Spark06_RDD_Operator_Transform_GroupBy_Test {

  def main(args: Array[String]): Unit = {

    //创建环境
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("RDD")
    val sparkContext = new SparkContext(sparkConf)

    //创建RDD算子 -- groupBy
    val rdd: RDD[String] = sparkContext.textFile("datas/1.txt")

    rdd.map(
      line => {
        val datas = line.split(" ")
        val time = datas(3)

        val sdf = new SimpleDateFormat("dd/MM/yyy:HH:mm:ss")
        val date = sdf.parse(time)
        val sdf1 = new SimpleDateFormat("HH")
        val hour: String = sdf1.format(date)
        ( hour ,1)

      }
    )groupBy(_._1)

  }
 }
